/**
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) 2010  Sean Beecroft

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 * @version 1.10 2010-01-01
 * @author Sean Beecroft, Jose M. Vidal
 */

package yawiki.client.browser;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;

/**
A simple Web Browser with minimal functionality.
@author Jose M. Vidal
*/
public class YBrowser extends JInternalFrame {

	public void setPage(String url){
		try {
			
			try { jep.setPage("http://www.yawiki.org/blank.html"); } catch(Exception ex){} 
			URL _url = new URL(url);
			URLConnection urlConnection = _url.openConnection();
			urlConnection.setDefaultUseCaches(false);
			jep.setPage(urlConnection.getURL());
			
			HTMLDocument doc = (HTMLDocument)jep.getDocument();
			jep.invalidate();
			jep.validate();
			jep.repaint();
		}
		catch (Exception e) {
			System.err.println(e);
			//System.exit(-1);
		}
	}
	
	
	public static boolean exists(String url){
		try {
			JEditorPane jEditor = new JEditorPane();
			jEditor.setPage(url);
			return true;
		}
		catch (IOException e) {
			System.err.println(e);
			//System.exit(-1);
			return false;
		}

	}

	/** An inner class which listens for keypresses on the Back button. */
	class backButtonListener implements ActionListener {
		protected JEditorPane jep;
		protected JLabel label;
		protected JButton backButton;
		protected Vector history;
		public backButtonListener(JEditorPane jep, JButton backButton, Vector history, JLabel label){
			this.jep = jep;
			this.backButton = backButton;
			this.history = history;
			this.label = label;
		}

		/** The action is to show the last url in the history.
		 @param e the event*/
		public void actionPerformed(ActionEvent e){
			try{
				//the current page is the last, remove it
				String curl = (String)history.lastElement();
				history.removeElement(curl);
					
				curl = (String)history.lastElement();
				System.out.println("Back to " + curl);
				setPage(curl);
				label.setText("<html><b>URL:</b> "+ curl);
				if (history.size() == 1)
					backButton.setEnabled(false);
			}
			catch (Exception ex){
				System.out.println("Exception " + ex);
			}
		}
	}

	/** An inner class that listens for hyperlinkEvent.*/
	class LinkFollower implements HyperlinkListener {
		protected JEditorPane jep;
		protected JLabel label;
		protected JButton backButton;
		protected Vector history;
		public LinkFollower(JEditorPane jep, JButton backButton, Vector history, JLabel label){
			this.jep = jep;
			this.backButton = backButton; 
			this.history = history;
			this.label = label;
		}
		/** The action is to show the page of the URL the user clicked on.
			@param evt the event. We only care when its type is ACTIVATED. */
		public void hyperlinkUpdate(HyperlinkEvent evt){
			if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
				try {
					String currentURL = evt.getURL().toString();
					history.add(currentURL);
					backButton.setEnabled(true);
					System.out.println("Going to " + currentURL);
					setPage(currentURL);
					label.setText("<html><b>URL:</b> "+ currentURL);
				}
				catch (Exception e) {
					System.out.println("ERROR: Trouble fetching url");
				}
			}
		}

	}

	JEditorPane jep = null;
	
	/** The contructor runs the browser. It displays the main frame with the
		fetched initialPage
		@param initialPage the first page to show */
	public YBrowser(){

		/** A vector of String containing the past urls */
		
		// set up the editor pane
		jep = new JEditorPane();
		jep.setEditable(false);

		// set up the window
		JScrollPane scrollPane = new JScrollPane(jep);     
		//JFrame f = new JFrame("Simple Web Browser");
		
		//jep.addHyperlinkListener(new LinkFollower(jep, backButton, history, label));
		add(scrollPane, BorderLayout.CENTER);
	}

	/** Create a Browser object. Use the command-line url if given */
	public static void main(String[] args) {
		//String initialPage = new String("http://www.cse.sc.edu");

		//if (args.length > 0) initialPage = args[0];

		
	}
	
}